package com.company;

import java.util.Arrays;

public class Main {
    //十六进制byte数据转化成10进制数
    public static int TwoByteToDecimal(byte a, byte b){
        return a * 16 + b;
    }
    public static void main(String[] args) {
        //TCPServer开启
        byte[] buffer = {0x6B,0x03,0x00,0x30,0x00,0x08,0x4D,0x09};
        TCPServer tcpServer = new TCPServer(2555) {
            @Override//设置接收数据的处理办法
            protected void onDataReceive(byte[] bytes, int size) {
                //将设备的数据转化成温湿度信息
                String content = "TCPServer say : " + Arrays.toString(bytes);
                System.out.println(content);
                System.out.println(
                        "Temprature : " + TwoByteToDecimal(bytes[3],bytes[4]) + "\n" +
                                "Humidity : " + TwoByteToDecimal(bytes[5],bytes[6])
                );
            }
        };
        tcpServer.setSendData(buffer);//设置发送给设备的指令
        tcpServer.setSleepTime(1100);//设置轮询间隔时间
        new Thread(()-> tcpServer.start()).start();

        //TCPServer停止
        tcpServer.stop();
    }
}